<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>空间分析服务-地形曲率计算</title>
    <script type="text/javascript" src="../../dist/include-openlayers.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    var map, options, terrainCurvatureCalculationResult,
        baseUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/map-jingjin/rest/maps/京津地区地图",
        serviceUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";
    var extent = [104.07, 30.54, 119.51, 42.31];
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [116.85, 39.79],
                zoom: 8,
                projection: "EPSG:4326",
                extent: extent
            })
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        terrainCurvatureCalculationProcess();
    });

    function terrainCurvatureCalculationProcess() {
        //创建地形曲率计算参数实例
        var terrainCurvatureCalculationParameters = new SuperMap.TerrainCurvatureCalculationParameters({
            dataset: "JingjinTerrain@Jingjin",
            zFactor: 1.0,
            averageCurvatureName: "CurvatureA",
            deleteExistResultDataset: true
        });
        //向iServer发起地形曲率计算请求
        new ol.supermap.SpatialAnalystService(serviceUrl).terrainCurvatureCalculate(terrainCurvatureCalculationParameters, function (serviceResult) {
            terrainCurvatureCalculationResult = serviceResult.result;
            //用栅格专题图展示分析结果
            showAnalysisResult_ThemeGridRange();
        });

        //构造栅格专题图
        function showAnalysisResult_ThemeGridRange() {
            var themeGridRangeIteme1 = new SuperMap.ThemeGridRangeItem({
                    start: -9000000,
                    end: -1000,
                    color: new SuperMap.ServerColor(0, 0, 255)
                }),
                themeGridRangeIteme2 = new SuperMap.ThemeGridRangeItem({
                    start: -1000,
                    end: -1,
                    color: new SuperMap.ServerColor(0, 0, 125)
                }),
                themeGridRangeIteme3 = new SuperMap.ThemeGridRangeItem({
                    start: -1,
                    end: 1,
                    color: new SuperMap.ServerColor(0, 125, 125)
                }),
                themeGridRangeIteme4 = new SuperMap.ThemeGridRangeItem({
                    start: 1,
                    end: 1000,
                    color: new SuperMap.ServerColor(0, 125, 0)
                }),
                themeGridRangeIteme5 = new SuperMap.ThemeGridRangeItem({
                    start: 1000,
                    end: 20000000,
                    color: new SuperMap.ServerColor(0, 255, 0)
                });

            var themeGridRange = new SuperMap.ThemeGridRange({
                reverseColor: false,
                rangeMode: SuperMap.RangeMode.EQUALINTERVAL,
                //栅格分段专题图子项数组
                items: [themeGridRangeIteme1,
                    themeGridRangeIteme2,
                    themeGridRangeIteme3,
                    themeGridRangeIteme4,
                    themeGridRangeIteme5
                ]
            });

            var themeParameters = new SuperMap.ThemeParameters({
                //制作专题图的数据集（地形曲率计算的结果数据集）
                datasetNames: [terrainCurvatureCalculationResult.averageCurvatureResult.dataset.split('@')[0]],
                dataSourceNames: ["Jingjin"],
                joinItems: null,
                themes: [themeGridRange]
            });

            new ol.supermap.ThemeService(baseUrl).getThemeInfo(themeParameters, function (serviceResult) {
                var result = serviceResult.result;
                if (result && result.newResourceID) {
                    var themeLayer = new ol.layer.Tile({
                        source: new ol.source.TileSuperMapRest({
                            url: baseUrl,
                            noWrap: true,
                            cacheEnabled: false,
                            transparent: true,
                            layersID: result.newResourceID,
                            tileGrid: new ol.tilegrid.TileGrid({
                                extent: extent,
                                resolutions: options.tileGrid.getResolutions()
                            }),
                            transparent: true
                        })
                    });
                    map.addLayer(themeLayer);
                }
            });
        }
    }
</script>
</body>
</html>